package de.eventdroid;

import java.util.ArrayList;
import java.util.StringTokenizer;

import de.eventdroid.helper_classes.JSONHandler;
import de.eventdroid.helper_classes.RESTHandler;
import android.app.Activity;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class EventSpecialResultScreen extends Activity {
	
	private ArrayList<String> aktFoundObjectsForListViewArray = new ArrayList<String>();
	private ArrayList<String> aktFoundObjectsIDs = new ArrayList<String>();
	private String resultString = null;
	private String specEventId = "";
	private StringTokenizer evtDataStrings;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.eventdroid_event_special_result);
        this.setSpecLocationFields();
    }
    
    /** Click Event Handler for the buttons on the Startscreen */
    public void onHomeClick(final View view) {
    	switch (view.getId()) {
    	case R.id.btn_event_special_result_screen_to_home:
    		startActivity(new Intent(this, StartScreen.class));
    		break;
    	}
    } 
    
    /**
     * Fill the Fields of the Activity Views with the result values
     */
    private void setSpecLocationFields() {
    	
    	/* Fetch assigned data of the special Location */
    	specEventId = getIntent().getExtras().getString("event_data");		
		
    	/* Fetch the data of the specialLocation from REST Server */
    	resultString = RESTHandler.getData("eventHandler/id/"+specEventId);
    	
		/* Parse the JSON String into  */
    	aktFoundObjectsForListViewArray = JSONHandler.parseJSONDataToArrayList(resultString, "event", true);
    	
    	/* Tokenizer for the fetched Data */
    	evtDataStrings = new StringTokenizer(aktFoundObjectsForListViewArray.get(0), "#");
    	
		try{
			/* TextViews which have to be set */
			((TextView)findViewById(R.id.lbl_spec_event_name)).setText(evtDataStrings.nextToken());
			((TextView)findViewById(R.id.lbl_spec_event_start_date)).setText(evtDataStrings.nextToken());
			((TextView)findViewById(R.id.lbl_spec_event_start_time)).setText(evtDataStrings.nextToken());
			((TextView)findViewById(R.id.lbl_spec_event_description)).setText(evtDataStrings.nextToken());
			evtDataStrings.nextToken(); 

			String event_location_id = evtDataStrings.nextToken();
			
			/* Fetch the data of the specialLocation from REST Server */
	    	resultString = RESTHandler.getData("eventHandler/allspeclocation/"+event_location_id);
	    	
	    	/* Parse the JSON String into  */
	    	aktFoundObjectsForListViewArray = JSONHandler.parseJSONDataToArrayList(resultString, "location", true);
	    	
	    	/* Tokenizer for the fetched Data */
	    	evtDataStrings = new StringTokenizer(aktFoundObjectsForListViewArray.get(0), "#");
			
	    	/* TextViews which have to be set */
			String location_name = evtDataStrings.nextToken();
			String location_adress = evtDataStrings.nextToken();
			String location_zip_town = evtDataStrings.nextToken() + " " + evtDataStrings.nextToken();
	    	
			((TextView)findViewById(R.id.lbl_spec_event_location)).setText(location_name);
			((TextView)findViewById(R.id.lbl_spec_event_location_address)).setText(location_adress);
			((TextView)findViewById(R.id.lbl_spec_event_location_town_combo)).setText(location_zip_town);
			
		} catch (ParseException e1) {
			Log.e("log_tag", "Parsing Exception: "+e1.toString());
		}
    	
    }
    
    /**
     * Creation Class for the menu
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
    	return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	
    	switch (item.getItemId()) {
    		case R.id.menu_startscreen:     
    			startActivity(new Intent(this, StartScreen.class));           	
    			break;
    	}
    	return true;
    }

}